import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import BigFile from '../components/bigFile.vue'
import FileTable from '../components/fileTable.vue'
import FileUpload from '../components/fileUpload.vue'
import Login from '../views/Login.vue'
import store from '@/store/index.js'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home,
    redirect: '/login',
    children: [
      {
        path: '/big_file',
        name: 'bigFile',
        component: BigFile,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/file_tree',
        name: 'fileTree',
        component: FileTable,
        meta: {
          requireAuth: true
        }
      },
      {
        path: '/file_upload',
        name: 'fileUpload',
        component: FileUpload,
        meta: {
          requireAuth: true
        }
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  }

]

const router = new VueRouter({
  routes
})
// 设置导航守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  }
  const userId = store.state.id
  if (!userId) return next('/login')
  next()
})
// 在路由中添加识别类meta作为是否需要检测用户登录状态的判断条件
// router.beforeEach((to, from, next) => {
//   if (to.meta.requireAuth) {
//     if (store.state.if) {
//       next()
//     } else {
//       next('/login')
//     }
//   } else {
//     next()
//   }
// })

export default router
